package org.jground.rx.ui.zk.report;

import java.io.ByteArrayOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jground.rx.IRXObject;
import org.jground.rx.report.RXColumn;
import org.jground.rx.report.RXReportPage;
import org.jground.rx.ui.zk.UIFrame;
import org.jground.rx.ui.zk.UIRXListboxModel;
import org.jground.rx.web.UIProperties;
import org.jground.util.CollectionUtils;
import org.jground.util.IOUtils;
import org.jground.util.StringUtils;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listhead;
import org.zkoss.zul.Listheader;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;

public class UIReportPage extends Listbox implements EventListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8749060300730853489L;

	private static Log logger = LogFactory.getLog(UIReportPage.class);

	private List<UIReportColumn> uiColumns = new ArrayList<UIReportColumn>();
	private UIFrame uiFrame;

	public UIReportPage(UIFrame uiFrame, UIReportPanel uiReportPanel,
			RXReportPage moReportPage, UIRXListboxModel model) {
		//
		logger.debug("init UIReportPage");
		this.uiFrame = uiFrame;
		Listhead head = new Listhead();
		head.setSizable(true);
		head.setParent(this);
		//
		Listheader lhr = null;
		// String sourceMoid = moReportPage.GetMOReport().getSourceMoid();
		for (RXColumn moColumn : moReportPage.GetRXColumns()) {
			// RXAttribute moAttribute = moColumn.getMOAttribute();
			UIReportColumn uiColumn = new UIReportColumn(uiFrame,
					uiReportPanel, moColumn);
			this.uiColumns.add(uiColumn);
			lhr = new Listheader(moColumn.getRXAttribute().getLabel());
			lhr.setParent(head);
			if (moColumn.isSortingEnabled()) {
				lhr.setSort("auto");
				lhr.setSortAscending(uiColumn.getAscendingComparator());
				lhr.setSortDescending(uiColumn.getDescendingComparator());
			}
		}
		//
		// reportRow = new UIReportRow(uiFrame, this.uiColumns,
		// this.uiReportPanel);
		this.setModel(model);
		this.setItemRenderer(new UIReportItemRenderer());
		this.setMold("paging");
		int nr = UIProperties.getInstance().getIntProperty(
				UIProperties.report_number_of_rows, 10);
		this.setPageSize(nr);
	}

	public List<UIReportColumn> getColumns() {
		return this.uiColumns;
	}

	public byte[] writeToCSVFile() throws Exception {
		ByteArrayOutputStream bout = null;
		PrintWriter out = new PrintWriter(new OutputStreamWriter(
				bout = new ByteArrayOutputStream(), IOUtils.UTF_8_ENCODING));
		for (int i = 0; i < this.getModel().getSize(); i++) {

			IRXObject rxObj = (IRXObject) getModel().getElementAt(i);
			out.print(rxObj.getOid());
			out.print(StringUtils.CSV_SEPARATOR);

			for (UIReportColumn column : uiColumns) {
				String s = CollectionUtils.join(
						column.renderToString(rxObj, false), ",");
				out.print(StringUtils.toCSV(s));
				out.print(StringUtils.CSV_SEPARATOR);
			}
			out.println();
		}
		IOUtils.closeQuietly(out);
		return bout.toByteArray();
	}

	class UIReportItemRenderer implements ListitemRenderer {

		public void render(Listitem item, Object data) throws Exception {
			Listcell cell = null;
			if (data instanceof IRXObject) {
				IRXObject mod = (IRXObject) data;
				for (UIReportColumn column : uiColumns) {
					cell = new Listcell();
					cell.setStyle("padding: 4px");
					cell.setParent(item);
					cell.setAttribute("oid", mod.getOid());
					addEventListener(mod, cell);
					Component c = column.getValueComponent(mod);
					c.setParent(cell);
				}
			}
		}

		private void addEventListener(IRXObject mod, Listcell cell) {
			if (mod.getRXClass().isTopLevel()) {
				cell.addEventListener(Events.ON_CLICK, UIReportPage.this);
			}
		}
	}

	public void onEvent(Event event) throws Exception {
		if (event.getTarget() instanceof Listcell) {
			Long oid = (Long) event.getTarget().getAttribute("oid");
			uiFrame.openView(oid, true);
		}
	}
}